<template>
    <div class="fastdata-dbsource-container">
        <el-dialog title="修改数据源" v-model="isShowDialog" >
            <el-form ref="editDbsourceFormRef" :model="formData" :rules="formRules" label-width="100px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源名称" prop="name">
                            <el-input v-model="formData.name" placeholder="请输入数据源名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源类型" prop="dbtype">
                            <el-select v-model="formData.dbtype" placeholder="请选择数据源类型" class="w100">
                                <el-option :key="1" label="MYSQL" :value="1"></el-option>
                                <el-option :key="2" label="ORACLE" :value="2"></el-option>
                                <el-option :key="3" label="SQLSERVER" :value="3"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="数据源驱动" prop="dbdriver">
                            <el-input v-model="formData.dbdriver" placeholder="请输入数据源驱动" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="数据源链接" prop="dburl">
                            <el-input v-model="formData.dburl" placeholder="请输入数据源链接" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="用户名称" prop="username">
                            <el-input v-model="formData.username" placeholder="请输入用户名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源密码" prop="password">
                            <el-input v-model="formData.password" placeholder="请输入数据源密码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源状态" prop="status">
                            <el-radio v-model="formData.status" label="0">正常</el-radio>
                            <el-radio v-model="formData.status" label="1">停用</el-radio>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { editDbsource } from '/@/api/fastdata/dbsource/index';
export default {
    name: 'fastdata/dbsource/edit',
    setup(props, context) {
        const editDbsourceFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {},
            formRules : {
                name: { required: true, message: '数据源名称不能为空', trigger: 'blur' },
            }
        });

        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            if(row){
                state.formData = Object.assign({}, row);
            }
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            editDbsourceFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            editDbsourceFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        id: state.formData.id,
                        name: state.formData.name,
                        dbtype: state.formData.dbtype,
                        dbdriver: state.formData.dbdriver,
                        dburl: state.formData.dburl,
                        username: state.formData.username,
                        password: state.formData.password,
                        status: state.formData.status,
                        remark: state.formData.remark,
                    }
                    editDbsource(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            editDbsourceFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>